package com.fitness.front.controller;

import com.fitness.front.convertor.CourseRateConvertor;
import com.fitness.front.modal.course.dto.CourseRateDto;
import com.fitness.front.modal.course.entity.CourseRate;
import com.fitness.front.modal.course.vo.CourseRateVO;
import com.fitness.front.modal.response.ServerResponseEntity;
import com.fitness.front.service.ICourseRateService;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

@RestController
@RequiredArgsConstructor
@RequestMapping("/api/v1/front/course-rate")
public class CourseRateController {

    private final ICourseRateService courseRateService;
    private final CourseRateConvertor courseRateConvertor;

    /**
     * 新增
     * @param courseRateDto
     * @return
     */
    @PostMapping("/add")
    public ServerResponseEntity<Boolean> add(@RequestBody CourseRateDto courseRateDto) {
        boolean result = courseRateService.addCourseRate(courseRateDto);

        if(result) {
            return ServerResponseEntity.success();
        }
        return ServerResponseEntity.fail("CR10001","添加评分失败");
    }

    /**
     * 获取课程表单
     */
    @GetMapping("/get")
    public ServerResponseEntity<CourseRateVO> getCourseRate(@RequestParam("userId") Long userId,
                                                            @RequestParam("courseId") Long courseId) {
        CourseRate result = courseRateService.getCourseRate(userId,courseId);
        if(result != null){
            CourseRateVO courseRateVO = courseRateConvertor.entity2Vo(result);
            courseRateVO.setTimestamp(result.getCreateTime().getTime());
            return ServerResponseEntity.success(courseRateVO);
        }
        return ServerResponseEntity.success(null);
    }
}
